<?php

namespace app\admin\model\advertiser;

use aliyun\AliyunOss;
use app\BaseModel;
use think\model\relation\HasOne;

/**
 * 广告主推广任务模型
 */
class AdvertiserTask extends BaseModel
{

    /**
     * 获取微信推广任务列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $orderby
     * @return array
     */
    public static function getWechatList(array $where, int $page, int $limit, string $orderby = ''): array
    {
        $data = self::where($where)
            ->with(['advertiserUser' => function ($query) {
                $query->field('id,phone,name');
            }, 'advertiserMatter' => function ($query) {
                $query->field('id,appid,ad_user_id,type,name,desc,avatar,qrcode');
            }])
            ->order($orderby)
            ->paginate(['list_rows' => $limit, 'page' => $page], false)
            ->toArray();
        //遍历数组
        $AliyunOss = new AliyunOss();
        foreach ($data['data'] as &$v) {
            //处理设备类型和投放地址
            $v['device_type'] = json_decode($v['device_type'], true) ?: [];
            $v['location'] = json_decode($v['location'], true) ?: [];
            //推广物
            if (isset($v['advertiserMatter'])) {
                $v['advertiserMatter']['avatar_url'] = $AliyunOss->getObjectUrl($v['advertiserMatter']['avatar']);
                $v['advertiserMatter']['qrcode_url'] = $AliyunOss->getObjectUrl($v['advertiserMatter']['qrcode']);
            }
        }
        return $data;
    }

    /**
     * 获取广告位推广任务列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $orderby
     * @return array
     */
    public static function getPageList(array $where, int $page, int $limit, string $orderby = ''): array
    {
        $data = self::where($where)
            ->with(['advertiserUser' => function ($query) {
                $query->field('id,phone,name');
            }, 'advertiserPage' => function ($query) {
                $query->field('id,code,name,pictures,example,desc,device_type,status');
            }])
            ->order($orderby)
            ->paginate(['list_rows' => $limit, 'page' => $page], false)
            ->toArray();
        //遍历数组
        $AliyunOss = new AliyunOss();
        foreach ($data['data'] as &$v) {
            //处理设备类型和投放地址
            $v['device_type'] = json_decode($v['device_type'], true) ?: [];
            $v['location'] = json_decode($v['location'], true) ?: [];
            //处理图片
            $v['pictures'] = json_decode($v['pictures'], true);
            $v['pictures_url'] = [];
            if ($v['pictures']) {
                foreach ($v['pictures'] as $kp => $pv) {
                    $v['pictures_url'][$kp] = $pv;
                    $v['pictures_url'][$kp]['img'] = $AliyunOss->getObjectUrl($pv['img']);
                }
            }
            //推广物
            if (isset($v['advertiserPage'])) {
                $v['advertiserPage']['example_url'] = $AliyunOss->getObjectUrl($v['advertiserPage']['example']);
                $v['advertiserPage']['device_type'] = json_decode($v['advertiserPage']['device_type'], true);
            }
        }
        return $data;
    }

    /**
     * 获取排序列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $orderby
     * @return array
     */
    public static function getSortList(array $where, int $page, int $limit, string $orderby = ''): array
    {
        $field = 'id,type,task_name,task_desc,status,orderby';
        $data = self::field($field)
            ->where($where)
            ->order($orderby)
            ->paginate(['list_rows' => $limit, 'page' => $page], false)
            ->toArray();
        return $data;
    }

    /**
     * 公众号和小程序推广任务分发
     * @param int $type
     * @return array
     */
    public static function wechatTaskDistribution(int $type): array
    {
        $field = 'a.id,a.single_price,a.daily_limit,a.total_limit,a.total_price,a.complete,b.name,b.appid,b.auth,b.auth_info';
        $where[] = ['a.type', '=', $type];
        $where[] = ['a.status', '=', 5];
        $where[] = ['b.auth', '=', 1];
        $data = self::alias('a')
            ->field($field)
            ->where($where)
            ->join((new AdvertiserMatter())->getTable() . ' b', 'a.matter_id = b.id')
            ->order('a.orderby', 'asc')
            ->find();
        return $data ? $data->toArray() : [];
    }

    public static function wechatTaskDistributionNew(int $type): array
    {
        $field = 'a.id,a.single_price,a.daily_limit,a.total_limit,a.total_price,a.complete,b.name,b.appid,b.auth,b.auth_info';
        $where[] = ['a.type', '=', $type];
        $where[] = ['a.status', '=', 5];
        $where[] = ['b.auth', '=', 1];
        $data = self::alias('a')
            ->field($field)
            ->where($where)
            ->join((new AdvertiserMatter())->getTable() . ' b', 'a.matter_id = b.id')
            ->order('a.orderby', 'asc')
            ->select()->toArray();
        return $data??[] ;
    }

    /**
     * 关联广告主表
     * @return HasOne
     */
    public function advertiserUser(): HasOne
    {
        return $this->hasOne(AdvertiserUser::class, 'id', 'ad_user_id');
    }

    /**
     * 关联广告微信推广物表
     * @return HasOne
     */
    public function advertiserMatter(): HasOne
    {
        return $this->hasOne(AdvertiserMatter::class, 'id', 'matter_id');
    }

    /**
     * 关联广告位推广物表
     * @return HasOne
     */
    public function advertiserPage(): HasOne
    {
        return $this->hasOne(AdvertiserPage::class, 'id', 'matter_id');
    }


}